package com.qingke.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.qingke.factory.ConnectionFactory;
import com.qingke.pojo.Project;
import com.qingke.pojo.User;

public class ProjectOperation implements JDBCOperation {
	private static Connection conn = ConnectionFactory.getConnection();
	private  PreparedStatement ps = null;
	@Override
	public boolean delete(Object obj) {
        Project pro = null;
		
		if(obj instanceof Project){
			 pro = (Project)obj;
		}
		try {
			ps = conn.prepareStatement("delete from project where code = ?");
			ps.setString(1, pro.getCode());
			return !ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("delete failed");
		}
		return false;
	}

	@Override
	public boolean update(Object obj) {
        Project pro = null;
		
		if(obj instanceof Project){
			 pro = (Project)obj;
		}
		try {
			ps = conn.prepareStatement("update project set name = ?,description = ?,apply_time = ?,user_id = ? where code = ?");
			ps.setString(1, pro.getName());
			ps.setString(2, pro.getDescription());
			ps.setString(3, pro.getApplytime());
			ps.setInt(4, pro.getUser_id());
			ps.setString(5, pro.getCode());
			return !ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("update failed");
		}
		return false;
	}

	@Override
	public boolean insert(Object obj) {
        Project pro = null;
		
		if(obj instanceof Project){
			 pro = (Project)obj;
		}
		try {
			ps = conn.prepareStatement("insert into project(code,name,description,apply_time,user_id) values(?,?,?,?,?) ");
			ps.setString(1, pro.getCode());
			ps.setString(2, pro.getName());
			ps.setString(3, pro.getDescription());
			ps.setString(4, pro.getApplytime());
			ps.setInt(5, pro.getUser_id());
			
			return !ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("insert failed");
		}
		return false;
	}

	@Override
	public ResultSet select(Object obj) {
        Project pro = null;
		
		if(obj instanceof Project){
			 pro = (Project)obj;
		}
		try {
			ps = conn.prepareStatement("delete from project where code = ?");
			ps.setString(1, pro.getCode());
			return ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("select failed");
		}
		return null;
	}

}
